package woaini.fenger.core.bot.base;

import cn.hutool.extra.spring.SpringUtil;
import lombok.Getter;
import lombok.Setter;
import woaini.fenger.core.bot.communication.base.BaseBotRequest;
import woaini.fenger.core.bot.config.BotConfig;
import woaini.fenger.core.bot.enums.BotStatusEnum;
import woaini.fenger.core.bot.enums.ConnectType;
import woaini.fenger.core.bot.enums.PLatFormEnum;
import woaini.fenger.core.bot.event.BaseEvent;
import woaini.fenger.core.bot.id.ID;
import woaini.fenger.core.bot.manager.key.BotKey;
import woaini.fenger.core.dispatcher.BotDispatcher;
import woaini.fenger.core.exception.BotAssertTool;
import woaini.fenger.core.exception.enums.BotExceptionType;

/**
 * 机器人抽象顶层
 *
 * @see Bot
 * @author yefeng {@code @Date} 2023-05-16 16:50:39
 */
@Getter
@Setter
public abstract class Bot {

  /**
   * @see BotStatusEnum 机器人状态
   */
  private BotStatusEnum status;

  private BotConfig botConfig;

  /**
   * @see ID ID id + 平台做为唯一标识 发送信息以这个为准
   */
  private ID selfId;

  /**
   * @see PLatFormEnum 平台
   */
  private PLatFormEnum pLatForm;

  /**
   * @see String 协议
   */
  private String agreement;

  /** 头像 */
  private String avatar;

  /**
   * @see String 昵称
   */
  private String nickName;

  /**
   * @see IBotAdapter 消息转换器
   */
  private IBotAdapter botAdapter;


  /**
   * @see ConnectType 接收类型
   */
  private ConnectType receiveType;

  /**
   * @see ConnectType 发送类型
   */
  private ConnectType sendType;

  public IBotAdapter getBotAdapter() {
    BotAssertTool.isActualTrue(botAdapter == null, BotExceptionType.BOT_CONFIG, "bot未配置适配器!");
    return botAdapter;
  }

  public ConnectType getReceiveType() {
    BotAssertTool.isActualTrue(this.receiveType== null, BotExceptionType.BOT_CONFIG, "bot未配置接收消息协议类型!");
    return receiveType;
  }

  public ConnectType getSendType() {
    BotAssertTool.isActualTrue(this.sendType == null, BotExceptionType.BOT_CONFIG, "bot未配置发送消息协议类型!");
    return sendType;
  }

  public Bot(BotConfig botConfig) {
    this.selfId = botConfig.getSelfId();
    this.pLatForm = botConfig.getPlatForm();
    this.agreement = botConfig.getAgreement();
    this.botConfig = botConfig;
    // 默认值
    this.status = BotStatusEnum.UNKNOWN;
  }

  /**
   * @MethodName online
   *
   * @author yefeng {@date 2024-01-17 18:11:28}
   * @since 1.0
   *     <p>在线
   */
  public void online() {
    this.status = BotStatusEnum.ONLINE;
  }

  public void offline() {
    this.status = BotStatusEnum.OFFLINE;
  }

  /**
   * @MethodName conncet
   *
   * @author yefeng {@date 2024-01-18 13:58:08}
   * @since 1.0
   *     <p>连接
   */
  public abstract void conncet();

  /**
   * @MethodName reConncet
   *
   * @author yefeng {@date 2024-01-18 13:58:11}
   * @since 1.0
   *     <p>重新连接
   */
  public abstract void reConncet();

  /**
   * @MethodName receive
   *
   * @param message 讯息
   * @author yefeng {@date 2024-01-18 16:59:32}
   * @since 1.0
   *     <p>接收消息处理 打包为 session发送
   */
  public void receive(BaseEvent message) {
    BotDispatcher dispatcher = SpringUtil.getBean(BotDispatcher.class);
    dispatcher.dispatch(this, message);
  }

  /**
   * @MethodName send
   *
   * @param messages 消息
   * @author yefeng {@date 2024-01-18 17:55:10}
   * @since 1.0
   *     <p>发送
   */
  public abstract void send(BaseBotRequest messages);

  public BotKey getKey(){
    return new BotKey(this.selfId, this.pLatForm);
  }
}
